package com.gp.billsrecord.base

import android.app.Application
import androidx.datastore.preferences.core.edit
import androidx.lifecycle.AndroidViewModel
import com.gp.billsrecord.model.datastore.ConstDataStore
import kotlinx.coroutines.flow.firstOrNull

/*数据处理逻辑送所在的类的基类*/
open class BaseViewModel(application: Application) : AndroidViewModel(application) {
    val TAG get() = this::class.simpleName ?: "NONE"
    /*更新文件中保存的用户ID（表示当前用户的id）*/
    suspend fun updateUserId(userId: Long) {
        ConstDataStore.get(getApplication()).edit { preferences ->
            preferences[Const.KEY_USER_ID] = userId
        }
    }
    /*取当前登录的用户ID*/
    suspend fun obtainUserId(): Long? {
        return ConstDataStore.get(getApplication()).data.firstOrNull()?.get(Const.KEY_USER_ID)
    }
}